from server import db, api_auth, user_opt, app_auth
from flask import jsonify, Blueprint, request, g
from server.model.m_dict import Dict
from server.model import set_field, query_by_field
import math

a_dict = Blueprint('app/dict', __name__)
exclude_data = {'pay_status', 'dict_no', 'actual_paid', 'security_code', 'user_id', 'express_status'}


@a_dict.route('', methods=['GET'])
def get_dict():
    """
    @@@
    #### Args:
    {
        page: 1,
        limit: 20,
        filter: {},
        start_time: "2021-09-10",
        end_time: "2021-09-12",
    }
    #### Returns::
    {
        'code': 20000,
        'items': [u.to_dict() for u in dict_list],
    }
    """
    page = request.args.get('page', type=int, default=1)
    limit = request.args.get('limit', type=int, default=20)
    start_time = request.args.get('start_time')
    end_time = request.args.get('end_time')
    query_filter = request.args.get('filter')

    try:
        dict_list = Dict.query.filter(Dict.name.in_(["每月金额", "小记者每月金额", "次年每月金额"]))

        if query_filter:
            dict_list = query_by_field(dict_list, query_filter, Dict)

        if start_time:
            dict_list = dict_list.filter(db.cast(Dict.create_time, db.Date) >= start_time)

        if end_time:
            dict_list = dict_list.filter(db.cast(Dict.create_time, db.Date) <= end_time)

        total = dict_list.count()
        dict_list = dict_list.offset((page - 1) * limit).limit(limit).all()

        return jsonify({
            'code': 20000,
            'message': 'success',
            'items': [u.to_dict(exclude=exclude_data) for u in dict_list],
            'totalCount': total,
            'TotalPageCount': math.ceil(int(total / limit))
        })
    except Exception as e:
        print("get dicts error:", e)

    return jsonify({'code': 50001, 'message': "查询参数时发生错误"})


# @a_dict.route('', methods=['GET'])
# @app_auth.login_required
# def get_dicts():
#     """
#                     @@@
#                     #### Args:
#                            {
#                                 page: 1,
#                                 limit: 20,
#                                 filter: {},
#                                 start_time: "2021-09-10",
#                                 end_time: "2021-09-12",
#                             }
#                     #### Returns::
#                             {
#                                 'code': 20000,
#                                 'items': [u.to_dict() for u in dict_list],
#                             }
#                 """
#     page = request.args.get('page', type=int, default=1)
#     limit = request.args.get('limit', type=int, default=20)
#     start_time = request.args.get('start_time')
#     end_time = request.args.get('end_time')
#     query_filter = request.args.get('filter')
#
#     try:
#
#         dict_list = Dict.query.filter(Dict.user_id == g.user.id, Dict.pay_status == DictStatus.Paid)
#
#         if query_filter:
#             dict_list = query_by_field(dict_list, query_filter, Dict)
#
#         if start_time:
#             dict_list = dict_list.filter(db.cast(Dict.create_time, db.Date) >= start_time)
#
#         if end_time:
#             dict_list = dict_list.filter(db.cast(Dict.create_time, db.Date) <= end_time)
#         total = dict_list.count()
#
#         dict_list = dict_list.offset((page - 1) * limit).limit(limit).all()
#
#         return jsonify({
#             'code': 20000,
#             'message': 'success',
#             'items': [u.to_dict(exclude=exclude_data) for u in dict_list],
#             'totalCount': total,
#             'TotalPageCount': math.ceil(int(total / limit))
#         })
#     except Exception as e:
#         print("get dicts error:", e)
#
#     return jsonify({'code': 50001, 'message': "查询参数时发生错误"})

